package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.UserCart;
import org.csu.mypetstore.service.UserCartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UpdateUserCartServlet", value = "/UpdateUserCartServlet")
public class UpdateUserCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Account account=new Account();
        HttpSession session=request.getSession();
        response.setContentType("text/plain");
        account=(Account)session.getAttribute("accounts");
        String username=account.getUsername();
        String itemid=request.getParameter("itemId");
        String quantity=request.getParameter("quantity");

        UserCart userCart=new UserCart();
        userCart.setItemid(itemid);
        userCart.setNumber(Integer.parseInt(quantity));
        userCart.setUserid(username);
        UserCartService userCartService=new UserCartService();
        boolean haveItem=userCartService.selectCartList(userCart);

        //判断是否有，以判断是否更新
        //有则为错误，及更新
        PrintWriter out=response.getWriter();
        if(haveItem)
        {
            out.print("Not Exist");
        }
        else if(!haveItem)
        {
            out.print("Exist");
        }
        //out.print(userCart);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
